

let roleList = {
    0: [
        { path: "/platformmanagementstatistics" },
        { path: "/commoditystatistics" },
        { path: "/users" },
        { path: "/admin" },
        { path: "/admins" },
        { path: "/managers" },
        { path: "/shops" },
        { path: "/good" },
        { path: "/odergoods" },
        { path: '/orderservices' },
        { path: '/managersapply' },
        { path: '/shopsapply' },
        { path: '/goodsapply' },
        { path: '/server' }

    ],
    2: [
        { path: "/odergoods" },
        { path: "/orderservices"},
        { path: "/good" },
        { path: '/server' },
        { path: "/shops" },
        { path: "/commoditystatistics" },
    ],

    1: [
        { path: "/managers" },
        { path: "/managersapply" },
        { path: "/shopsapply" },
        { path: "/goodsapply" },
        { path: "/users" },
        { path: "/platformmanagementstatistics" },


    ],
};

/**
 *  role0 // 所有权限
 *  role1 // 没有操作日志权限
 *  role2 // 只有组织架构权限
 *  role3 // 只有日志权限
 *
 * -----------假设这是调用接口返回的权限列表-------------
 */
// url -----> admin、director、manager、staff
export const getRoleJson = (url) => {
    return new Promise(resolve => {
        return resolve(roleList[url] || roleList.admin);
    });
};


/**
 * 递归判断路由，删除没有的权限
 * @param {Array} tree // 系统所有路由树
 * @param {Array} roles // 后端返回的权限列表
 * @return Array
 */
export function _getRoleList(tree, roles) {
    let finalTree = [...tree];
    let fun = list => {
        for (let i = 0; i < list.length; i++) {
            let c = list[i];
            if (roles.findIndex(d => d.path == c.path) > -1 || c.hidden) {
                if (c.children && c.children.length) {
                    fun(c.children);
                }
            } else {
                let {
                    meta: { children }
                } = c;
                if (children && children.length) {
                    let isHave = children.findIndex(e => roles.findIndex(f => f.path == e) > -1);
                    // 如果不存在-删除，如果存在-继续递归
                    if (isHave == -1) {
                        list.splice(i, 1);
                        i--;
                    } else {
                        if (c.children && c.children.length) {
                            fun(c.children);
                        }
                    }
                } else {
                    list.splice(i, 1);
                    i--;
                }
            }
        }
    };
    fun(finalTree);
    return finalTree;
}